package com.gitee.ice1938.module.admin.app.manage.appbasetype;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.jdbc.core.BaseRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.QueryInfo;
import org.springframework.stereotype.Repository;
/**
 * AppBaseTypeController
 * 
 * 
 * @author ice1938@qq.com  2019-11-28 14:48:48 
 *
 */
@Repository
public class AppBaseTypeRepository extends BaseRepository {

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private QueryInfo querySql(Map<String, Object> param, Pageable pageable, Order order) {
		String keyword = param.get("q_search_key") == null ? null : param.get("q_search_key").toString().trim();
		List<Object> params = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder("SELECT ");
         //编码
 		sql.append("abt.type_code,"); 
         //名称
 		sql.append("abt.type_name");  
		//
		sql.append(" FROM app_base_type as abt ");
		//模糊查询模版	
       	if (keyword != null && !keyword.isEmpty()) {
			sql.append(" where ( abt.type_code like ?");
			params.add("%" + keyword + "%");
			sql.append(" or abt.type_name like ?)");
			params.add("%" + keyword + "%");
		}
		
		String sqlCount = builderCountSql(sql.toString());
		if (order != null) {
			sql.append(" ORDER BY ");
			sql.append(underscoreName(order.getProperty()));
			sql.append(" ");
			sql.append(order.getDirection());
		}
		if (pageable != null) {
			sql.append(" LIMIT ");
			sql.append(pageable.getPageNumber() * pageable.getPageSize());
			sql.append(", ");
			sql.append(pageable.getPageSize());
		}
		return new QueryInfo(sql.toString(),sqlCount,params);
		
	}
	public Page<AppBaseTypeEntity> findData(Map<String, Object> param, Pageable pageable, Order order) {
		QueryInfo sqlinfo = querySql(param,pageable,order);
		
	    List<AppBaseTypeEntity> content = jdbcTemplate.query(sqlinfo.getSql(),sqlinfo.getParams().toArray(), BeanPropertyRowMapper.newInstance(AppBaseTypeEntity.class));
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<AppBaseTypeEntity>(content, pageable, total);
		}
		return new PageImpl<AppBaseTypeEntity>(content);
    }

	public Page<Map<String, Object>> queryData(Map<String, Object> param, Pageable pageable, Order order) {
	    QueryInfo sqlinfo = querySql(param,pageable,order);
		
	    List<Map<String, Object>> content = jdbcTemplate.queryForList(sqlinfo.getSql(),sqlinfo.getParams().toArray());
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<Map<String, Object>>(content, pageable, total);
		}
		return new PageImpl<Map<String, Object>>(content);
    }

	public int update(AppBaseTypeEntity data,String userLoginName) {
	   	StringBuilder sql = new StringBuilder("UPDATE app_base_type  SET  "); 
	   	List<Object> params = new ArrayList<>();
  		//编码
 		sql.append("type_code = ?,"); 
        params.add(data.getTypeCode());
  		//名称
 		sql.append("type_name = ?");  
        params.add(data.getTypeName());
	  
		sql.append(" WHERE ");
  		//编码
 		sql.append("type_code = ?");  
		 params.add(data.getTypeCode()); 
		int update = jdbcTemplate.update(sql.toString(),  params.toArray());
		return update;
    }

	public int insert(AppBaseTypeEntity data,String userLoginName) {
		
	    StringBuilder sql = new StringBuilder("INSERT INTO app_base_type ( "); 
	   	List<Object> params = new ArrayList<>();
  		//编码
 		sql.append("type_code ,"); 
        params.add(data.getTypeCode());
  		//名称
 		sql.append("type_name");  
        params.add(data.getTypeName());
	  
		sql.append(" ) VALUES ( ");
 		sql.append("? ,"); 
 		sql.append("? ");  
		sql.append(")");
		int update = jdbcTemplate.update(sql.toString(),  params.toArray());
		return update;
    }

	public int delete(String typeCode) {
	     StringBuilder sql = new StringBuilder(" DELETE FROM app_base_type WHERE "); 
	   	List<Object> params = new ArrayList<>();
		sql.append("type_code = ?");  
		params.add(typeCode); 
		int update = jdbcTemplate.update(sql.toString(),  params.toArray());
		return update;
    }
    
	public int delete(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return 0;
		}
	    StringBuilder sql = new StringBuilder("DELETE FROM app_base_type WHERE "); 
	   	List<Object> params = new ArrayList<>();
 		sql.append("type_code in ("); 
		for (int i = 0; i < ids.size(); i++) {
			String id = ids.get(i);
			if (i == 0) {
				sql.append("?");
			} else {
				sql.append(",?");
			}
			params.add(id);
		}
		sql.append(")");
		int update = jdbcTemplate.update(sql.toString(),  params.toArray());
		return update;
    }

}
